// we7/pages/fenLei/fenLeiList.js
const app = getApp();
Page({
  data: {
    page:1,
    limit:4,
    optionsId:'',
    data:[],
    order_type:'',
    goodsList:'',
    nomorelist:false,
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    inputValue:'',
    kaishi:1
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options){
    


    var that = this;
    var inputValue = options.inputVal
    var arr = new Array;
    // 数据存储
    if (!wx.getStorageSync('historyInputVal')){
      arr[0] = inputValue;
      wx.setStorageSync('historyInputVal', arr);
    }else{
      arr = wx.getStorageSync('historyInputVal');
      var str = '';
      for(var i=0;i<arr.length;i++){
        str += arr[i];
      }
      var reg = RegExp(options.inputVal);
      if (!reg.test(str)){
        var idx = arr.length;
        arr[idx] = options.inputVal;
        wx.setStorageSync('historyInputVal', arr);
      }
    }
    if (inputValue){
      app.util.request({
        url: 'admin/WxGoodsApi/searchGoods',
        data: {
          page: this.data.page,
          limit: this.data.limit,
          order_type: 3,
          search_name: inputValue
        },
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success:function(res){
          var data = res.data.data;
          for(var i = 0; i < data.length; i++){
            data[i]['goods_list_image'] = app.globalData.url + data[i]['goods_list_image'];
          }
          that.setData({
            order_type: 3,
            goodsList: res.data.data,
            inputValue: inputValue
          })
        }
      });
    }else{
    this.setData({
      ccc:"#000"
    });
    
    app.util.request({
      url: 'admin/WxGoodsApi/goodsList',
      data:{
        id: options.id,
        page: this.data.page,
        limit: this.data.limit,
        order_type: 3
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res){
        var data = res.data.data
        for (var i = 0; i < data.length; i++){
          data[i]['goods_list_image'] = app.globalData.url + data[i]['goods_list_image'];
        }
        that.setData({
          order_type: 3,
          goodsList: res.data.data,
          optionsId:options.id
        })
      }
    });
   }
  },
  // 最新
  zuiNew: function(data){
    var that = this;
    this.check(data);
    if (that.data.inputValue){
      app.util.request({
        url: 'admin/WxGoodsApi/searchGoods',
        data: {
          search_name: that.data.inputValue,
          page: 1,
          limit: that.data.limit,
          order_type: data.target.dataset.style
        },
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res){
          var data = res.data.data
          for (var i = 0; i < data.length; i++) {
            data[i]['goods_list_image'] = app.globalData.url + data[i]['goods_list_image'];
          }
          that.hideModal();
          that.setData({
            goodsList: res.data.data,
            data: data
          })
        }
      });
    }else{
      app.util.request({
        url: 'admin/WxGoodsApi/goodsList',
        data: {
          id: that.data.optionsId,
          page: 1,
          limit: that.data.limit,
          order_type: data.target.dataset.style
        },
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var data = res.data.data
          for (var i = 0; i < data.length; i++) {
            data[i]['goods_list_image'] = app.globalData.url + data[i]['goods_list_image'];
          }
          that.hideModal();
          that.setData({
            goodsList: res.data.data,
            data: data
          })
        }
      });
    }
  },

  check:function(data){
    var that = this;
    var id = data.currentTarget.dataset.id;
    var ccc = "#666666";
    var bbb = "#666666";
    var aaa = "#666666";
    var price_sheng = "#666";
    var price_jiang="#666";
    if(id==1){
      ccc="#000";
      that.setData({
        order_type: data.currentTarget.dataset.style
      })
    }
    if(id == 2){
      bbb = "#000";
      that.setData({
        order_type: data.currentTarget.dataset.style
      })
    }
    if(id == 3){
      aaa = "#000";
    }
    if (id == 4){
      price_sheng = "orange";
      that.setData({
        order_type: data.currentTarget.dataset.style
      })
    }
    if (id == 5){
      price_jiang="orange";
      that.setData({
        order_type: data.currentTarget.dataset.style
      })
    }
    that.setData({
      bbb:bbb,
      ccc:ccc,
      aaa:aaa,
      price_sheng: price_sheng,
      price_jiang: price_jiang
    })
  },
  showModal1:function(){
    this.setData({
      bbb: "#000",
      ccc: "#666666"
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function(){
    var page = this.data.page;
    var pages = ++page;
    var that = this;
    that.setData({
      page: pages
    })
    if(that.data.inputVal){
      app.util.request({
        url: 'admin/WxGoodsApi/searchGoods',
        data: {
          search_name: that.data.inputVal,
          page: pages,
          limit: that.data.limit,
          order_type: that.data.order_type
        },
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var data = res.data.data;
          if (!data) {
            that.setData({
              nomorelist: true
            })
          } else {
            for (var i = 0; i < data.length; i++) {
              data[i]['goods_list_image'] = app.globalData.url + data[i]['goods_list_image'];
            }
            that.hideModal();
            var goodsList = that.data.goodsList;
            for (var i = 0; i < goodsList.length; i++) {
              data.push(goodsList[i]);
            }
            that.setData({
              goodsList: data,
              data: data
            })
          }
        },
        fail: function (res) {
          console.log("页面有错哦")
        }
      });
    }else{
      app.util.request({
        url: 'admin/WxGoodsApi/goodsList',
        data: {
          id: that.data.optionsId,
          page: pages,
          limit: that.data.limit,
          order_type: that.data.order_type
        },
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var data = res.data.data;
          if (!data) {
            that.setData({
              nomorelist: true
            })
          } else {
            for (var i = 0; i < data.length; i++) {
              data[i]['goods_list_image'] = app.globalData.url + data[i]['goods_list_image'];
            }
            that.hideModal();
            var goodsList = that.data.goodsList;
            for (var i = 0; i < goodsList.length; i++) {
              data.push(goodsList[i]);
            }
            that.setData({
              goodsList: data,
              data: data
            })
          }
        },
        fail: function (res) {
          console.log("页面有错哦")
        }
      });
    }
    
  }
})